# class Solution(object):
#     def isValid(self, s):
#         """
#         :type s: str
#         :rtype: bool
#         """
#         str1=[]
#         str2=[]
#         for i in range(len(s)):
#             str=s[i]
#             if str=="(" or str=="[" or str=="{":
#                 str1=str1.append(str)
#             if str==")" or str=="]" or str=="}":
#                 str1=str1.pop()
#         if str1==:
#             return True
#         else:
#             return False
# solution=Solution()
# s="()"
# print(solution.isValid(s))

# class Solution(object):
#     def isValid(self, s):
#         stack = []  # 使用一个栈来存储左括号
#         mapping = {')': '(', ']': '[', '}': '{'}  # 映射右括号到对应的左括号
#
#         for char in s:
#             if char in '([{':
#                 stack.append(char)  # 遇到左括号，直接入栈
#             elif char in ')]}':
#                 if not stack:
#                     return False  # 栈为空，无法匹配当前右括号
#                 top = stack.pop()  # 弹出栈顶元素
#                 if mapping[char] != top:
#                     return False  # 类型不匹配，无效
#             else:
#                 # 非法字符，题目保证输入有效，可以省略此分支
#                 return False
#
#         # 遍历结束后，栈为空表示所有括号都正确匹配
#         return len(stack) == 0
#
#
# # 测试代码
# solution = Solution()
# print(solution.isValid("()"))  # 输出 True
# print(solution.isValid("()[]{}"))  # 输出 True
# print(solution.isValid("(]"))  # 输出 False
# print(solution.isValid("([)]"))  # 输出 False
# print(solution.isValid("{[]}"))  # 输出 True

class Solution(object):
    def isValid(self, s):
        stack=[]
        maping={')':'(','}':'{',']':'['}#字典
        for char in s:
            if char in '({[':
                stack.append(char)#入栈
            elif char in ')}]':
                if not stack:
                    return False
                top=stack.pop()
                if maping[char]!=top:
                    return False
        if len(stack)==0:
            return True

solution=Solution()
s="()"
print(solution.isValid(s))
